<script setup lang="ts">
import moment from 'moment'
import { onBeforeUnmount, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

const $router = useRouter()

const timeFormat = 'YYYY年MM月DD日 HH:mm:ss'
const time = ref<string>(moment().format(timeFormat))
let updateTimer: any = null

const goHome = () => {
  $router.push('/home')
}

onMounted(() => {
  updateTimer = setInterval(() => {
    time.value = moment().format(timeFormat)
  }, 1000)
})

onBeforeUnmount(() => {
  if (updateTimer) {
    clearInterval(updateTimer)
  }
})
</script>
<script lang="ts">
export default {
  name: 'Top',
}
</script>
<template>
  <div class="top">
    <div class="left">
      <div class="btn btn_lt" @click="goHome">首页</div>
    </div>
    <div class="center">
      <div class="title">智慧旅游可视化大数据平台</div>
    </div>
    <div class="right">
      <div class="btn btn_rt"><span>统计报告</span></div>
      <span class="time">当前时间 {{ time }}</span>
    </div>
  </div>
</template>

<style scoped lang="scss">
$top-height: 40px;
$title-height: 74px;
$btn-width: 150px;

.top {
  display: flex;
  width: 100%;
  height: $top-height;
  font-size: 18px;
  color: #29fcff;

  .left,
  .right {
    flex: 1;
    background: url('@/assets/images/data_screen/dataScreen-header-left-bg.png')
      no-repeat;
    background-size: cover;
  }

  .center {
    flex: 2;

    .title {
      width: 100%;
      height: $title-height;
      font-size: 30px;
      line-height: $title-height;
      text-align: center;
      background: url('@/assets/images/data_screen/dataScreen-header-center-bg.png')
        no-repeat;
      background-size: cover;
    }
  }

  .btn {
    width: $btn-width;
    height: $top-height;
    line-height: $top-height;
    text-align: center;
    cursor: pointer;
    background: url('@/assets/images/data_screen/btn.png') no-repeat;
    background-size: cover;

    &.btn_lt {
      float: right;
    }

    &.btn_rt {
      float: left;
      transform: scaleX(-1);

      span {
        display: inline-block;
        transform: scaleX(-1);
      }
    }
  }

  .right {
    .time {
      float: right;
      margin-right: 5px;
      line-height: $top-height;
    }
  }
}
</style>
